﻿using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
using UnityEngine.Networking.Match;

namespace demo2
{
    public class NetManagerHub: MonoBehaviour
    {
        public string serverIp ="192.168.8.209";
        public Manager manager;

        void Awake()
        {
            DontDestroyOnLoad(gameObject);
        }
		public int use_typeid =0;
        bool isStart = false;
        void OnGUI()
        {
			if (!isStart) {
				if (GUI.Button (new Rect (20, 40, 100, 20), "StartHost")) {
					StartHost ();
					isStart = true;
					use_typeid = 0;
				}
				if (GUI.Button (new Rect (20, 80, 100, 20), "StartServer")) {
					StartServer ();
					isStart = true;
					use_typeid = 1; 
				}
				if (GUI.Button (new Rect (20, 120, 150, 20), "Start Client,serverIp:")) {
					JoinLocalGame ();
					isStart = true;
					use_typeid = 2; 
				}
				serverIp = GUI.TextArea (new Rect (200, 120, 120, 20), serverIp);
                
			} /*else {
				if (GUI.Button (new Rect (20, 40, 100, 20),(use_typeid==0?"StopHost":(use_typeid==1?"StopServer":"StopClient")))) {
					if (use_typeid == 0) {
						manager.StopHost ();
					} else if (use_typeid == 1) {
						manager.StopServer ();
					} else if (use_typeid == 2) {
						manager.StopClient ();
					}
				}
			}*/
            

        }

        public void StartHost()
        {
            ConnectionConfig config = new ConnectionConfig();
            config.AddChannel(QosType.ReliableSequenced);
			config.AddChannel(QosType.Unreliable);//服务质量（Qos)等级
            NetworkServer.Configure(config, 100);
            manager.StartHost(config, 100);
            Debug.LogFormat(string.Format("Host started"));


        }

        public void StartServer()
        {
            ConnectionConfig config = new ConnectionConfig();
            config.AddChannel(QosType.ReliableSequenced);
            config.AddChannel(QosType.Unreliable);
            NetworkServer.Configure(config, 100);
			manager.StartHost(config, 100);

            Debug.LogFormat(string.Format("Server started"));
        }

        public void JoinLocalGame()
        {
            manager.networkAddress = serverIp;

            manager.StartClient();

            ConnectionConfig config = new ConnectionConfig();
            config.AddChannel(QosType.ReliableSequenced);
            config.AddChannel(QosType.Unreliable);
            manager.client.Configure(config, 100);
            manager.client.RegisterHandler(MsgType.Disconnect, OnDisconnected);
            manager.client.RegisterHandler(MsgType.Error, OnError);

            Debug.LogFormat(string.Format("connecting to {0} ", serverIp));
        }

        void OnError(NetworkMessage netMsg)
        {
            Debug.LogError("lobby client on error "+ netMsg.msgType);
        }
        private void OnDisconnected(NetworkMessage netMsg)
        {
            Debug.LogError("lobby OnDisconnected "+ netMsg.msgType);
            //*_*UnityEngine.SceneManagement.SceneManager.LoadScene(DEF.OfficeLineSceneName);
        }



        void OnLevelWasLoaded()
        {
            //GameMain.ClearGuiString();
        }

    }
}
